package com.efesco.system;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.efesco.common.Tools;

/**
 * function_templateRightDatasetListener
 */
public class function_templateRightDatasetListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
  	String superFunctionId=Tools.toStr(dataset.parameters().getString("super_function_id"));
  	String functionTemplateId=Tools.toStr(dataset.parameters().getString("functionTemplateId"));
  	String sql="";
  	if (!superFunctionId.equals("") &&  !superFunctionId.equals("null"))  {
  		sql="select sf.*,1 isChecked from s_function sf,s_function_template_right tr "+
			"where tr.function_template_id="+functionTemplateId+" and sf.function_id=tr.function_id "+  
			"and sf.super_function_id="+superFunctionId+ 
			" union "+ 
			"select sf.*,0 isChecked from s_function sf where sf.super_function_id="+superFunctionId+ 
			" and sf.function_id not in "+ 
			"(select function_id from s_function_template_right where "+ 
			"function_template_id="+functionTemplateId+" )";
  	}
  	else  {
  		sql="select sf.*,1 isChecked from s_function sf,s_function_template_right tr "+
			"where tr.function_template_id="+functionTemplateId+" and sf.function_id=tr.function_id "+  
			"and sf.super_function_id is null "+ 
			" union "+ 
			"select sf.*,0 isChecked from s_function sf where sf.super_function_id is null "+ 
			" and sf.function_id not in "+ 
			"(select function_id from s_function_template_right where "+ 
			"function_template_id="+functionTemplateId+" )";
  	}
	SqlDataset sqlDataset=(SqlDataset)dataset;
	sqlDataset.setSql(sql);
    return true;
  }

}
